
import { isEmpty } from './valid'
//本地存储
// ========== Token 相关 ==========
const AccessTokenKey = "ACCESS_TOKEN";

export function getToken() {
    return localStorage.getItem(AccessTokenKey);
}

export function setToken(token: string) {
    localStorage.setItem(AccessTokenKey, token);
}

export function removeToken() {
    localStorage.removeItem(AccessTokenKey);
}

//===========用户信息================  
const UserKey = 'USER'
export function getUser() {
    const userObj = localStorage.getItem(UserKey)
    if (userObj != null) return JSON.parse(userObj)
}
export function setUser(user: any) {
    if (isEmpty(user)) return
    return localStorage.setItem(UserKey, JSON.stringify(user))
}

//===========字典相关数据==============
const DictKey = "DICT";

export interface DictValue {
    type: string;
    values: any;
}
//设置字典项
export function setDict(dictData: DictValue[]) {
    //DictValue [] 后面带一个[]表示数组
    const data: Record<string, any> = {}; // Record<string, any>键为字符串，值为任意类型的对象。
    for (let i = 0; i < dictData.length; i++) {
        const type: string = dictData[i]?.type;
        if (type) {
            data[type] = dictData[i].values;
        }
    }
    localStorage.setItem(DictKey, JSON.stringify(data));
}
//获取字典项
export function getDict(type: string = "") {
    const storedData = localStorage.getItem(DictKey);
    const data = storedData ? JSON.parse(storedData) : {};
    if (type) return data[type];
    return data;
}
